package com.zlsy.learning.leetcode.stack.s682;

import java.util.Stack;

/**
 * @author zhouliang
 * @date 2020/8/12 14:07
 */
public class CalPoints1 {
  public static void main(String[] args) {
    //    String[] ops = new String[] {"5", "2", "C", "D", "+"};
    String[] ops = new String[] {"5", "-2", "4", "C", "D", "9", "+", "+"};
    int i = calPoints(ops);
    System.out.println(i);
  }

  public static int calPoints(String[] ops) {
    Stack<Integer> stack = new Stack();
    for (String op : ops) {
      if (op.equals("+")) {
        int top = stack.pop();
        int newtop = top + stack.peek();
        stack.push(top);
        stack.push(newtop);
      } else if (op.equals("C")) {
        stack.pop();
      } else if (op.equals("D")) {
        stack.push(2 * stack.peek());
      } else {
        stack.push(Integer.valueOf(op));
      }
    }
    int ans = 0;
    for (int score : stack) {
      ans += score;
    }
    return ans;
  }
}
